using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ProgramMain.UniversalTariffPlans;
using DevExpress.XtraEditors;
using ProgramMain.Framework;
using ProgramMain.Properties;

namespace ProgramMain.PhoneTariffPlans
{
    public partial class PhoneTariffDirEditorCtl : BaseDetailCtl
    {
        public PhoneTariffDirEditorCtl()
        {
            InitializeComponent();
        }

        private Nullable<Int64> m_ServiceGroup_ID = new Nullable<Int64>();
        public Nullable<Int64> ServiceGroup_ID
        {
            get
            {
                return m_ServiceGroup_ID;
            }
            set
            {
                m_ServiceGroup_ID = value;
            }
        }

        public bool bIsOutgoing
        {
            get
            {
                return phoneTariffDirListCtl1.bIsOutgoing;
            }
            set
            {
                phoneTariffDirListCtl1.bIsOutgoing = value;
                phoneTariffPropertiesCtl1.bIsOutgoing = value;
            }
        }

        private void PhoneDirEditorCtl_DetailIDChanged(object sender, EventArgs e)
        {
            phoneTariffDirListCtl1.EnableEdit = this.EnableEdit;
            phoneTariffDirListCtl1.DetailID = this.DetailID;
            phoneTariffRateListCtl1.EnableEdit = this.EnableEdit;
            phoneTariffRateListCtl1.UniversalTariffPlanID = this.DetailID;
            phoneTariffPropertiesCtl1.DetailID = this.DetailID;
        }

        private void phoneTariffDirListCtl1_ViewRowIDChanged(object sender, ShowDetailEventArgs e)
        {
            phoneTariffRateListCtl1.EnableEdit = e.ShowMode == ShowDetailModes.Edit;
            phoneTariffRateListCtl1.DetailID = phoneTariffDirListCtl1.ViewRowID;
            if (phoneTariffDirListCtl1.bIsDirectionOptimized.HasValue)
            {
                if (!phoneTariffDirListCtl1.bIsDirectionOptimized.Value)
                {
                    phoneTariffRateListCtl1.tabRateEditor.SelectedTabPageIndex = 0;
                    phoneTariffRateListCtl1.ChangeTariffDetailsViewGrouping(true);
                }
                else
                {
                    try
                    {
                        //ShowWaiting();
                        string strDescription = "";
                        this.queriesTableAdapter.QueryCommandTimeout = Settings.Default.LongQueryTimeout;
                        if (DetailID.HasValue && phoneTariffDirListCtl1.ViewRowID.HasValue)
                        {
                            this.queriesTableAdapter.qryPhoneTariffDirections_GenerateDescription(
                                (ServiceGroup_ID.HasValue) ? Convert.ToInt32(ServiceGroup_ID.Value) : (int?) null,
                                Convert.ToInt32(DetailID.Value),
                                phoneTariffDirListCtl1.ViewRowID,
                                ref strDescription);
                            phoneTariffRateListCtl1.memoRateView.Text = strDescription;
                        }
                    }
                    finally
                    {
                        //HideWaiting();
                    }
                    phoneTariffRateListCtl1.tabRateEditor.SelectedTabPageIndex = 1;
                    phoneTariffRateListCtl1.ChangeTariffDetailsViewGrouping(false);
                }
            }
            else
            {
                phoneTariffRateListCtl1.tabRateEditor.SelectedTabPageIndex = 0;
                phoneTariffRateListCtl1.ChangeTariffDetailsViewGrouping(true);
            }
        }

        virtual public void RefreshControl()
        {
            phoneTariffDirListCtl1.RefreshControl();
        }

        private void phoneTariffRateListCtl1_DetailIDEdited(object sender, EventArgs e)
        {
            phoneTariffDirListCtl1.ReloadViewRow();
        }
    }
}
